/**
 * @Author：ming
 * @Date：2023/9/30 8:47 PM
 * @Filename：Lc198
 */

/**
 * @author ming
 */
public class Lc198 {
    public int rob(int[] nums) {

        int len = nums.length;

        if(len==1){
            return nums[0];
        }

        int[] a = new int[len];
        boolean[] flag = new boolean[len];

        a[0] = nums[0];
        flag[0]=true;

        a[1] = Math.max(nums[0],nums[1] );
        if(a[1]==a[0]){
            flag[1]=false;
        }else {
            flag[1]=true;
        }


        for (int i = 2; i < len; i++) {
            if(flag[i-1]==false){
                flag[i] = true;
                a[i] = Math.max(a[i-1],a[i-2])+nums[i];
            }else {
                if(a[i-2]+nums[i]>a[i-1]){
                    a[i] = a[i-2]+nums[i];
                    flag[i] = true;
                }else {
                    a[i]=a[i-1];
                    flag[i]=false;
                }
            }

        }

        return a[len-1];
    }
}
